<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.Date" %>
<%--
用户登录后看到的第一个界面：
1.显示可以投票的投票项目(过期不显示,投过不显示)
2.显示已经投过票的项目(过期仍然显示，但加上已经已经过期的文字)
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>


<h1 style="color: #3880c2">
    <a href="index.jsp" style="color: crimson">返回主页面</a>
</h1>
<center>
    <h1>可选投票:</h1>
    <%
        request.setCharacterEncoding("UTF-8");
        //已经投票过的项目的id
        List<Integer> votedIdList = new ArrayList<Integer>();
        //已经投票过的项目的title和id，存入map;list不靠谱
        Map<Integer, String> voteMap = new HashMap<Integer, String>();


        int userId = Integer.parseInt(session.getAttribute("userId").toString());
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
            //这是一个连表查询语句，可以查询到这个用户投过哪些vote项目，以便后面展示的时候区分
            PreparedStatement preparedStatement = connection.prepareStatement("select vote_id from vote_option v\n" +
                    "inner join user_option u\n" +
                    "on u.option_id=v.id\n" +
                    "where u.user_id=" + userId);
            ResultSet resultSetEd = preparedStatement.executeQuery();
            if (!resultSetEd.next()) {
                System.out.print("该用户还没有投过票!!!");
            } else {
                //将所有结果保存到votedId
                do {
                    int id = resultSetEd.getInt(1);
                    votedIdList.add(id);
                } while (resultSetEd.next());
            }
            resultSetEd.close();
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            out.print(e.getMessage());
            e.printStackTrace();
        }


    %>

    <%

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
            PreparedStatement preparedStatement = connection.prepareStatement("select * from vote");
            ResultSet resultSet = preparedStatement.executeQuery();
            if (!resultSet.next()) {
                out.print("暂无数据!");
            } else {
                try {
                    /* 获取明天的日期代码->*/
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new java.util.Date());
                    // 将日期加一天
                    calendar.add(Calendar.DAY_OF_YEAR, -1);
                    java.util.Date tomorrow = calendar.getTime();
                    /*获取明天的日期代码->*/

                    //查询全部也可用,先do再while循环
                    //展示所有已经创建投票，只展示标题，附带跳转页面效果，自带id
                    do {
                        int id = resultSet.getInt(1);
                        String title = resultSet.getString(2);
                        Date date = resultSet.getDate(3);

                        //需要先执行这一个，避免已经过期了但是是被投票过的项目
                        //chatGpt出来的，id等于voteId中的任意一项就不显示，必须里面是!=外面是!
                        //逻辑翻译：如果每一项都不等于id 的 非 有一项等于id
                        if (!votedIdList.stream().allMatch(item -> id != item)) {
                            System.out.println(title + "已经被用户" + userId + "投过票了");
                            //将这个投过票的标题存入voteMap，对应其id
                            voteMap.put(id, title);
                        }
                        //查看它是否过期,获取到的日期早于当前(仅仅精确到日期，如果相等则过期)
                        else if (date.before(tomorrow)) {
                            //过期，不显示
                            System.out.println(title + "已过期");
                        } else {
                            //未过期且没有被投过票,显示
                            out.write("<a style=\"font-size: large\" href=\"userVoting.jsp?id=" +
                                    id + "\">" + title + "</a>\n");
                            out.write("<br>");
                        }
                    } while (resultSet.next());
                } catch (Exception e) {
                    out.print(e.getMessage());
                    e.printStackTrace();
                }
            }
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            out.print(e.getMessage());
            e.printStackTrace();
        }
    %>
    <br>
    <br>
    <br>
    <br>
    <br>
    <h1>已投票的项目:</h1>
    <%
        if (!voteMap.isEmpty()) {
            //遍历map
            for (Map.Entry<Integer, String> entry : voteMap.entrySet()) {
                //已经被投过票的项目,显示
                out.write("<a style=\"font-size: large ; color: #e1892d\" href=\"userVoted.jsp?id=" +
                        entry.getKey() + "\">" + entry.getValue() + "</a>\n");
                out.write("<br>");
            }
        }
    %>
</center>
</body>
</html>
